package com.thread;

import java.util.Arrays;

public class SyncApiDemo2 {
    public static void main(String[] args) {
        //StringBuffer在多线程里拼接是安全的

        StringBuffer sbf = new StringBuffer();
        new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                sbf.append("abc");
            }
        }).start();

        new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                sbf.append("def");
            }
        }).start();

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println(sbf.length());
        System.out.println(sbf.toString());
    }
}
